define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'shop/message/index' + location.search,
                    add_url: 'shop/message/add',
                    edit_url: 'shop/message/edit',
                    del_url: 'shop/message/del',
                    multi_url: 'shop/message/multi',
                    import_url: 'shop/message/import',
                    table: 'shop_message',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'name',
                            title: __('Name'),
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        {
                            field: 'logo_image',
                            title: __('Logo_image'),
                            operate: false,
                            events: Table.api.events.image,
                            formatter: Table.api.formatter.image
                        },
                        {
                            field: 'lng',
                            title: __('Lng'),
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        {
                            field: 'lat',
                            title: __('Lat'),
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        {
                            field: 'create_time',
                            title: __('Create_time'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.datetime
                        },
                        // {
                        //     field: 'update_time',
                        //     title: __('Update_time'),
                        //     operate: 'RANGE',
                        //     addclass: 'datetimerange',
                        //     autocomplete: false,
                        //     formatter: Table.api.formatter.datetime
                        // },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons: [
                                // {
                                //     name: 'Goods',
                                //     text: __('Goods'),
                                //     title: __('Goods'),
                                //     extend: 'data-area=["100%","100%"]',
                                //     classname: 'btn btn-xs btn-primary btn-dialog',
                                //     url: 'shop/message/goods',
                                //     callback: function (data) {
                                //         Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                //     },
                                //     visible: function (row) {
                                //         //返回true时按钮显示,返回false隐藏
                                //         return true;
                                //     }
                                // },
                                // {
                                //     name: 'Board',
                                //     text: __('Board'),
                                //     title: __('Board'),
                                //     extend: 'data-area=["100%","100%"]',
                                //     classname: 'btn btn-xs btn-primary btn-dialog',
                                //     url: 'shop/board/index',
                                //     callback: function (data) {
                                //         Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                //     },
                                //     visible: function (row) {
                                //         //返回true时按钮显示,返回false隐藏
                                //         return true;
                                //     }
                                // },
                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        edit_stock: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        },
        goods: function () {
            // 初始化表格参数配置
            Table.api.init();
            this.table.first(Config.shop_id);
            this.table.second(Config.shop_id);
        },
        table: {
            first: function (shop_id) {
                // 表格1
                var table1 = $("#table1");

                //添加分类方法
                $('.add-category').click(function () {
                    var ids = Table.api.selectedids(table1);
                    if (ids.length <= 0) {
                        return layer.alert('Please select the category you want to add');
                    }
                    layer.confirm('Are you sure you want to add？', {
                        btn: ['Yes', 'No'] //按钮
                    }, function (index) {

                        $.ajax({
                            url: "shop/message/update_shop_category",
                            type: "post",
                            data: {ids: ids, shop_id: shop_id},
                            success: function (res) {
                                if (res.code == 0) {
                                    table1.bootstrapTable('refresh');
                                    layer.alert(res.msg)

                                } else {
                                    layer.alert(res.msg)
                                }
                            }
                        })
                        Layer.close(index);
                    }, function () {
                    });
                })

                //删除分类方法
                $('.del-category').click(function () {
                    var ids = Table.api.selectedids(table1);
                    if (ids.length <= 0) {
                        return layer.alert('Please select the category you want to add');
                    }
                    layer.confirm('Are you sure you want to delete？', {
                        btn: ['Yes', 'No'] //按钮
                    }, function (index) {

                        $.ajax({
                            url: "shop/shop/delete_shop_category",
                            type: "post",
                            data: {ids: ids, shop_id: Config.shop_id},
                            success: function (res) {
                                if (res.code == 0) {
                                    table1.bootstrapTable('refresh');
                                    layer.alert(res.msg)

                                } else {
                                    layer.alert(res.msg)
                                }
                            }
                        })
                        Layer.close(index);
                    }, function () {
                    });
                })

                table1.bootstrapTable({
                    url: 'shop/message/category?shop_id=' + shop_id,
                    toolbar: '#toolbar1',
                    sortName: 'id',
                    search: false,
                    pageSize: 100,
                    columns: [
                        [
                            {field: 'state', checkbox: true,},
                            // {field: 'id', title: 'ID'},
                            {field: 'name', title: __('Name')},
                            {
                                field: 'have', title: __('have'),
                                searchList: {100: "Not", 200: "Yes"},
                                formatter: Table.api.formatter.status
                            },
                            {
                                field: 'operate',
                                title: __('Operate'),
                                table: table1,
                                events: Table.api.events.operate,
                                buttons: [
                                    {
                                        name: 'Show',
                                        title: 'Show',
                                        text: 'Show',
                                        classname: 'btn btn-primary btn-xs btn-click',
                                        click: function (e, data) {
                                            $("#myTabContent2 .form-commonsearch input[name='category_id']").val(data.id);
                                            $("#myTabContent2 .btn-refresh").trigger("click");
                                        }
                                    }
                                ],
                                formatter: Table.api.formatter.operate
                            }
                        ]
                    ]
                });

                // 为表格1绑定事件
                Table.api.bindevent(table1);
            },
            second: function (shop_id) {
                // 表格2
                var table2 = $("#table2");


                $('.add-goods').click(function () {
                    var ids = Table.api.selectedids(table2);
                    if (ids.length <= 0) {
                        return layer.alert('Please select the goods you want to add');
                    }
                    layer.confirm('Are you sure you want to add？', {
                        btn: ['Yes', 'No'] //按钮
                    }, function (index) {

                        $.ajax({
                            url: "shop/message/add_shop_goods",
                            type: "post",
                            data: {ids: ids, shop_id: shop_id},
                            success: function (res) {
                                if (res.code == 0) {
                                    table2.bootstrapTable('refresh');
                                    layer.alert(res.msg)

                                } else {
                                    layer.alert(res.msg)
                                }
                            }
                        })
                        Layer.close(index);
                    }, function () {
                    });
                })
                $('.del-goods').click(function () {
                    var ids = Table.api.selectedids(table2);
                    if (ids.length <= 0) {
                        return layer.alert('Please select the goods you want to add');
                    }
                    layer.confirm('Are you sure you want to delete？', {
                        btn: ['Yes', 'No'] //按钮
                    }, function (index) {

                        $.ajax({
                            url: "shop/message/delete_shop_goods",
                            type: "post",
                            data: {ids: ids, shop_id: shop_id},
                            success: function (res) {
                                if (res.code == 0) {
                                    table2.bootstrapTable('refresh');
                                    layer.alert(res.msg)

                                } else {
                                    layer.alert(res.msg)
                                }
                            }
                        })
                        Layer.close(index);
                    }, function () {
                    });
                })


                table2.bootstrapTable({
                    url: 'shop/message/goods_list?shop_id=' + shop_id,
                    extend: {
                        index_url: '',
                        add_url: '',
                        edit_url: '',
                        del_url: '',
                        multi_url: '',
                        table: '',
                    },
                    toolbar: '#toolbar2',
                    sortName: 'id',
                    search: false,
                    columns: [
                        [
                            {field: 'state', checkbox: true,},
                            {field: 'id', title: 'ID'},
                            {
                                field: 'have', title: __('have'),
                                searchList: {100: "Not", 200: "Yes"},
                                formatter: Table.api.formatter.status
                            },
                            {field: 'name', title: __('Name')},
                            {
                                field: 'image', title: __('Avatar'),
                                events: Table.api.events.image,
                                formatter: Table.api.formatter.image
                            },
                            {field: 'category_id', visible: false, title: __('Category')},
                            {field: 'price', title: __('Price'), operate: false,},
                            // {field: 'url', title: __('Url'), align: 'left', formatter: Table.api.formatter.url},
                            // {field: 'ip', title: __('ip')},
                            // {
                            //     field: 'createtime',
                            //     title: __('Createtime'),
                            //     formatter: Table.api.formatter.datetime,
                            //     operate: 'RANGE',
                            //     addclass: 'datetimerange',
                            //     sortable: true
                            // },
                        ]
                    ]
                });

                // 为表格2绑定事件
                Table.api.bindevent(table2);
            }
        },
        stock: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'shop/message/stock' + location.search,
                    table: 'shop_goods',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {field: 'id', title: __('Id')},
                        {
                            field: 'shop.name',
                            title: __('Shop name'),
                            operate: 'LIKE',
                            table: table,
                        },
                        {
                            field: 'goods.name',
                            title: __('Goods name'),
                            operate: 'LIKE',
                        },
                        {
                            field: 'category_name',
                            title: __('Category'),
                            operate: false,
                        },
                        {
                            field: 'category_id',
                            title: __('Category'),
                            addClass: 'selectpage',
                            extend: 'data-source="goods/goods_category/index"',
                            visible:false
                        },
                        {
                            field: 'stock',
                            title: __('Stock'),
                            operate: false,
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        {
                            field: 'price',
                            title: __('Price'),
                            operate: false,
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        // {
                        //     field: 'update_time',
                        //     title: __('Update_time'),
                        //     operate: 'RANGE',
                        //     addclass: 'datetimerange',
                        //     autocomplete: false,
                        //     formatter: Table.api.formatter.datetime
                        // },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'Stock',
                                    text: __('Edit'),
                                    title: __('Edit'),
                                    extend: 'data-area=["80%","80%"]',
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    url: 'shop/message/edit_stock',
                                    callback: function (data) {
                                        Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                    },
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        return true;
                                    }
                                },

                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

    };
    return Controller;
});
